package com.houxy.controller;

import com.houxy.repository.mapper.ExpertMapper;
import com.houxy.repository.model.bo.ExpertBO;
import com.houxy.repository.model.bo.UserBO;
import com.houxy.repository.model.entity.Expert;
import com.houxy.service.ExpertService;
import com.houxy.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Hou
 * @date 2019/3/19 10:30
 * @describe：
 */
@Controller
@RequestMapping("/expert")
public class ExpertController {

    @Autowired
    private ExpertService expertService;

    @Autowired
    private ExpertMapper expertMapper;

    @Autowired
    private RegisterService registerService;


    @RequestMapping("/updateExpert")
    public String updateExpert(Expert expert, HttpSession session) {
        int b = expertMapper.updateByPrimaryKeySelective(expert);
        if (b > 0) {
            session.setAttribute("result", "修改信息成功，重新登录后生效！");
            return "common/msg";
        }
        session.setAttribute("result", "修改信息失败，请重试或联系管理员！");
        return "common/msg";
    }

    @RequestMapping("/upload")
    public String upload(MultipartFile file, HttpServletRequest httpServletRequest) throws IOException {
        String newFileName = new Date().getTime() + file.getOriginalFilename().substring(file.getOriginalFilename().indexOf("."));
        String filePath = httpServletRequest.getServletContext().getRealPath("upload") + "//" + newFileName;
        File dstFile = new File(filePath);
        file.transferTo(dstFile);
        httpServletRequest.getSession().setAttribute("newFileName", newFileName);
        httpServletRequest.getSession().setAttribute("file", file);
        httpServletRequest.getSession().setAttribute("filePath", "/upload" + "/" + newFileName);
        return "upload/upload_re";

    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().removeAttribute("currentExper");
        return "";
    }

    @RequestMapping("/findExpertByHospitalId")
    public ModelAndView findExpertByHospitalId(@RequestParam("hospitalId") int hospitalId, HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        List<ExpertBO> expertList = expertService.findByHospitalId(hospitalId);
        modelAndView.addObject("expertList", expertList);
        modelAndView.setViewName("front/expert/expertByHospital");
        return modelAndView;
    }

    @RequestMapping("/findExpertById")
    public ModelAndView findExpertById(@RequestParam("expertId") int expertId, HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        ExpertBO expertBO = expertService.findAllById(expertId);
        modelAndView.addObject("expert", expertBO);
        modelAndView.setViewName("front/expert/expertDetail");
        return modelAndView;
    }

    @RequestMapping("/searchExpert")
    public ModelAndView searchExpert(@RequestParam(value = "hospitalId", required = false) Integer hospitalId,
                                     @RequestParam(value = "departmentId", required = false) Integer departmentId,
                                     @RequestParam(value = "type", required = false) String type,
                                     @RequestParam(value = "name", required = false) String name) {
        ModelAndView modelAndView = new ModelAndView();
        List<ExpertBO> expertlist = expertService.findAllByCondition(hospitalId, departmentId, type, name);
        modelAndView.addObject("expertList", expertlist);
        modelAndView.setViewName("front/expert/expertByHospital");
        return modelAndView;
    }

    @RequestMapping("/findUserByExpertId")
    public ModelAndView findUserByExpertId(int expertId) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("front/expert/expertByHospital");
        return modelAndView;
    }

    @RequestMapping("/registerManager")
    public ModelAndView registerManager(Integer expertId) {
        List<UserBO> userBOList = new ArrayList<>();
        if (expertId != null) {
            userBOList = registerService.findUserByExpertId(expertId);
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("registerExpertList", userBOList);
        modelAndView.setViewName("front/expert/expertRegisterManager");
        return modelAndView;
    }

}
